﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApplicationGenerator.BusinessModule.Interfaces;
using System.Reflection;
using ApplicationGenerator.CommonModule;

namespace ApplicationGenerator.CommonForms.Forms
{
    public partial class MappingsForm : Form
    {
        private Mappings ConfiguredMappings
        {
            get;
            set;
        }

        public string WebServiceClientDll
        {
            private get;
            set;
        }

        public string WebServiceMethodName
        {
            private get;
            set;
        }

        public IMetaModel MetaModel
        {
            private get;
            set;
        }

        public MappingsForm(IMetaModel metaModel, string webServiceClientDll, string webServiceMethodName)
        {
            ConfiguredMappings = new Mappings();
            MetaModel = metaModel;
            WebServiceClientDll = webServiceClientDll;
            WebServiceMethodName = webServiceMethodName;
            InitializeComponent();
            InitializeMappingsControls();
        }

        private void InitializeMappingsControls()
        {
            MethodInfo methodInfo = FindMethod();
            SetReturnParameter(methodInfo);
            SetInParameters(methodInfo);
        }

        private MethodInfo FindMethod()
        {
            Assembly webServiceClientAssembly = Assembly.LoadFrom(WebServiceClientDll);
            foreach (var type in webServiceClientAssembly.GetTypes())
            {
                if (type.GetMethod(WebServiceMethodName) != null)
                {
                    return type.GetMethod(WebServiceMethodName);
                }
            }

            throw new Exception("Nie ma metody");
        }

        private void SetReturnParameter(MethodInfo methodInfo)
        {
            Type returnType = methodInfo.ReturnType;
            if (returnType == typeof(void))
            {
                return;
            }

            ConfiguredMappings.ReturnParameter = new MappingEntity();
            ConfiguredMappings.ReturnParameter.ParameterName = "Wyjście";

            ParameterInConfigurationControl parameterControl = new ParameterInConfigurationControl(MetaModel);
            parameterControl.ParameterName = "Wyjście";
            parameterControl.ParameterType = returnType;

            groupBoxOutput.Controls.Add(parameterControl);
            parameterControl.Dock = DockStyle.Fill;

        }

        private void SetInParameters(MethodInfo methodInfo)
        {
            ParameterInfo[] inParameters = methodInfo.GetParameters();
            tableLayoutPanel1.RowStyles.Clear();

            if (inParameters.Length == 0)
            {
                return;
            }

            for (int counter = 0; counter < inParameters.Length; counter++)
            {
                tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            }


            for (int counter = 0; counter < inParameters.Length; counter++)
            {
                MappingEntity newParameter = new MappingEntity();
                newParameter.ParameterName = inParameters[counter].Name;
                ConfiguredMappings.InParameteres.Add(newParameter);
                ParameterInConfigurationControl parameterControl = new ParameterInConfigurationControl(MetaModel);
                parameterControl.ParameterName = newParameter.ParameterName;
                parameterControl.ParameterType = inParameters[counter].ParameterType;
                tableLayoutPanel1.Controls.Add(parameterControl, 0, counter);
            }
        }

        public void InitializeMappings(Mappings mappings)
        {
            (groupBoxOutput.Controls[0] as ParameterInConfigurationControl).MappedField = mappings.ReturnParameter.MappedField;
            for (int counter = 0; counter < tableLayoutPanel1.Controls.Count; counter++)
            {
                (tableLayoutPanel1.Controls[counter] as ParameterInConfigurationControl).MappedField = mappings.InParameteres[counter].MappedField;
            }
        }

        public Mappings GetConfiguredMappings()
        {
            Mappings configuredMappings = new Mappings();
            ParameterInConfigurationControl parameterControl = groupBoxOutput.Controls[0] as ParameterInConfigurationControl;
            configuredMappings.ReturnParameter = new MappingEntity();
            configuredMappings.ReturnParameter.ParameterName = parameterControl.ParameterName;
            configuredMappings.ReturnParameter.MappedField = parameterControl.MappedField;

            foreach (ParameterInConfigurationControl inParameterControl in tableLayoutPanel1.Controls)
            {
                MappingEntity newParameter = new MappingEntity();
                newParameter.ParameterName = inParameterControl.ParameterName;
                newParameter.MappedField = inParameterControl.MappedField;
                configuredMappings.InParameteres.Add(newParameter);
            }

            return configuredMappings;
        }
    }
}
